package org.benjamin.lending.service.impl;

import java.sql.Timestamp;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.benjamin.lending.domain.entity.MemberInfo;
import org.benjamin.lending.domain.entity.ReplaceBankLog;
import org.benjamin.lending.repository.MemberInfoRepository;
import org.benjamin.lending.repository.ReplaceBankLogRepository;
import org.benjamin.lending.service.BankService;
import org.benjamin.lending.service.TrusteeshipService;
import org.springframework.beans.factory.annotation.Autowired;

public class BankServiceImpl implements BankService {
	
	public final Logger logger = LogManager.getLogger();

	@Autowired
	private MemberInfoRepository memberInfoRepository;
	
	@Autowired
	private ReplaceBankLogRepository replaceBankLogRepository;
	
	@Autowired
	private TrusteeshipService trusteeshipService;
	
	@Override
	public void saveAndSubmitReplaceBankCrad(String reason, String username) {
		ReplaceBankLog replaceBankLog = new ReplaceBankLog();
		MemberInfo memberInfo = this.memberInfoRepository.findByUsername(username);
		replaceBankLog.setMemberInfo(memberInfo);
		replaceBankLog.setReason(reason);
		replaceBankLog.setStatus(0);
		replaceBankLog.setCreateTime(new Timestamp(System.currentTimeMillis()));
		this.replaceBankLogRepository.save(replaceBankLog);
		logger.info("解除安全充值卡已经成功提交到后台等待，工作人员审核！");
	}
	
	@Override
	public void submitTreasip(Integer id) throws RuntimeException {
		ReplaceBankLog replaceBankLog = this.replaceBankLogRepository.findOne(id);
		this.trusteeshipService.replaceBankCrad(replaceBankLog.getReason(), replaceBankLog.getMemberInfo());
		replaceBankLog.setStatus(1);
		this.replaceBankLogRepository.save(replaceBankLog);
	}

}
